from urllib import request, parse


if __name__ == "__main__":
    url = 'http://httpbin.org/post'
    data = dict(a=1, b='哈哈')
    data_bytes = bytes(parse.urlencode(data), encoding='utf8')
    req = request.Request(url=url, data=data_bytes, method='POST')
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36')
    req.add_header('Host', 'httpbin.org')
    # 若接收请求的url并不支持post方式的请求。则这里可能会抛出405,Not Allowed异常
    response = request.urlopen(req)
    print(response.read().decode("utf-8"))